/**
 * 
 */
package com.changil.nowonchangilchurch.ui.item;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.text.Html;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.changil.nowonchangilchurch.R;
import com.changil.nowonchangilchurch.protocol.dto.AttachmentImage;
import com.changil.nowonchangilchurch.ui.layout.ArticleImageViewPager;
import com.changil.nowonchangilchurch.util.DateUtil;
import com.changil.nowonchangilchurch.widget.asyncimagegetter.HtmlSetter;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * @author 홍창우
 * 
 */
public class ArticleItem extends RelativeLayout {
	private Context mContext = null;
	private RelativeLayout mLayout = null;
	private TextView mAuthor = null;
	private TextView mPublishDate = null;
	private TextView mArticleTitle = null;
	private TextView mArticleContent = null;

	private ArticleImageViewPager mInnerList = null;

	/**
	 * @param context
	 */
	public ArticleItem(Context context) {
		super(context);
		init(context);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public ArticleItem(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public ArticleItem(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private void init(Context context) {
		this.setLayoutParams(new AbsListView.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayout = (RelativeLayout) inflater.inflate(R.layout.article_item,
				this, true);
		mAuthor = (TextView) mLayout.findViewById(R.id.article_item_author);
		mPublishDate = (TextView) mLayout
				.findViewById(R.id.article_item_published_date);
		mArticleTitle = (TextView) mLayout
				.findViewById(R.id.article_item_article_title);
		mArticleContent = (TextView) mLayout
				.findViewById(R.id.article_item_article_content);
		mInnerList = (ArticleImageViewPager)mLayout.findViewById(R.id.article_item_content_view_pager);
	}

	public void setAuthor(CharSequence author) {
		if (null != author) {
			mAuthor.setText(author);
		}
	}

	public void setPublishDate(Date publishDate) {
		if (null != publishDate) {
			mPublishDate
					.setText(DateUtil.getPublishDate(mContext, publishDate));
		}
	}

	public void setArticleTitle(CharSequence title) {
		if (null != title) {
			mArticleTitle.setText(title);
		}
	}

	public void setArticleContent(String content) {
		if (null != content) {
			HtmlSetter.newInstanceWithoutImageLoad(mContext, mArticleContent, content).setTextViewHTML(false);
			String str = mArticleContent.getText().toString().replaceAll(" ", "").toString();
			str = Html.toHtml(new SpannableString(str)).replaceAll("&#65532;", "");
			str = Html.fromHtml(str).toString();
			if("".equals(str)) {
				mArticleContent.setVisibility(View.GONE);
			}else {
				mArticleContent.setVisibility(View.VISIBLE);
			}
		}
	}

	private void setInnerList(ArrayList<AttachmentImage> list) {
		if (null != list) {
			mInnerList.setVisibility(View.VISIBLE);
			mInnerList.clear();
			mInnerList.addList(list);
			mInnerList.setAdapter();
		} else {
			mInnerList.clear();
			mInnerList.clearAdapter();
			mInnerList.setVisibility(View.GONE);
		}
	}

	public void setInnerList(String articleId, ArrayList<AttachmentImage> list,
			ListView scroll, boolean mUseBusyMode, boolean isBusy) {
		mInnerList.setArticleId(articleId);
		mInnerList.setIsBusy(isBusy);
		mInnerList.setUseBusyMode(mUseBusyMode);
		setInnerList(list);
		mInnerList.setDisallowInterceptListView(scroll);
	}

	public void setInnerListUserActionListener(
			ArticleImageViewPager.UserActionListener l) {
		if (null != mInnerList && null != l) {
			mInnerList.setUserActionListener(l);
		}
	}

}
